package com.boyuan.aboutcase;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import com.boyuan.huijiaoyu.R;
import com.example.contactfragment.ContactGroupService;
import com.magicsoft.app.entity.CaseDetailEntity;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.CaseService;
import com.magicsoft.app.wcf.listener.GetMultilParamsListener;
import com.magicsoft.app.wcf.listener.PostRecordResponseListener;
import com.magicsoft.constant.Constant;

public class NewCaseDetailActivity extends Activity implements OnClickListener {

	private TextView mTv_jump_priority;
	private TextView mTv_jump_change_status;
	private EditText mEdi_case_title;
	private EditText mEdi_case_desc;
	private TextView mTv_case_type;
	private String caseTitle;
	private CaseService caseService;
	private CaseDetailEntity caseDetailEntity;
	private String tag = "NewCaseDetailActivity";
	private ContactGroupService contactgroupService;
	private String groupId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_case_detail);
		findViewById(R.id.rootview_casechange).setOnClickListener(this);
		caseDetailEntity = (CaseDetailEntity) getIntent().getSerializableExtra("caseDetailEntity");
		caseTitle = getIntent().getStringExtra("CaseTitle");
		groupId = getIntent().getStringExtra("groupId");
		initUI();
	}

	private void initUI() {
		findViewById(R.id.lay_back_casechange).setOnClickListener(this);
		findViewById(R.id.lay_ok).setOnClickListener(this);

		mEdi_case_title = (EditText) findViewById(R.id.edi_case_title);
		mEdi_case_desc = (EditText) findViewById(R.id.edi_case_desc);
		mTv_case_type = (TextView) findViewById(R.id.tv_case_type);
		mTv_jump_priority = (TextView) findViewById(R.id.tv_jump_priority);
		mTv_jump_change_status = (TextView) findViewById(R.id.tv_jump_change_status);

		mTv_jump_priority.setOnClickListener(this);
		mTv_jump_change_status.setOnClickListener(this);

		if (caseDetailEntity != null) {
			mEdi_case_title.setText(caseDetailEntity.getTitle());
			mEdi_case_desc.setText(caseDetailEntity.getDesc());
			mTv_case_type.setText(caseDetailEntity.getType_name());
			String priority = caseDetailEntity.getPriority();
			if(priority != null && !"".equals(priority)){
				setPriority1(priority, mTv_jump_priority);
			}
			String status = caseDetailEntity.getStatus();
			if (status != null && !"".equals(status)) {
				switch (Integer.valueOf(status)) {
				case 1:
					mTv_jump_change_status.setText("未处理");
					break;
				case 2:
					mTv_jump_change_status.setText("处理中");
					break;
				case 3:
					mTv_jump_change_status.setText("已关闭");
					break;
				case 4:
					mTv_jump_change_status.setText("等待客户确认");
					break;
				case 5:
					mTv_jump_change_status.setText("已完成");
					break;
				default:
					break;
				}
			}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finish();
		return super.onTouchEvent(event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lay_back_casechange:
			finish();
			break;
		case R.id.tv_jump_priority:
			jump_priority();
			break;
		case R.id.tv_jump_change_status:
			tv_jump_change_status();
			break;
		case R.id.lay_ok:
//			if ("修改case".equals(caseTitle)) {
			case_save();
//			} else {
//				newcase();
//			}
			break;
		case R.id.rootview_casechange:
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
			break;
		default:
			break;
		}
	}

	private String getStatusNum(String status) {
		if ("未处理".equals(status)) {
			return "1";
		} else if ("处理中".equals(status)) {
			return "2";
		} else if ("已关闭".equals(status)) {
			return "3";
		} else if ("等待客户确认".equals(status)) {
			return "4";
		} else if ("已完成".equals(status)) {
			return "5";
		}

		return "1";
	}

	private void case_save() {
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		String corp_id = sp.getString("accountId", "");
		String realname = sp.getString("realname", "");
		final String title = mEdi_case_title.getText().toString().trim();
		String desc = mEdi_case_desc.getText().toString().trim();
		String priority = mTv_jump_priority.getText().toString().trim();
		String status = mTv_jump_change_status.getText().toString().trim();
		final String statusNum = getStatusNum(status);
		String type_id = caseDetailEntity.getType_id();
		String cid = caseDetailEntity.getCid();
		final String groupid = caseDetailEntity.getGid();
		if (caseService == null) {
			caseService = new CaseService(this);
		}

		caseService.ChangeCaseDetail(corp_id, cid, title, 
				desc, type_id, setPriority(priority), statusNum, corp_id, realname,
				new PostRecordResponseListener() {

					@Override
					public void onFinish(String succString) {
//						Intent intent = new Intent();
//						intent.setAction(Constant.NOTITYHOMELIST);
//						sendBroadcast(intent);
//						Intent intent1 = new Intent();
//						intent1.setAction(Constant.NOTITYCASEDETAIL);
//						sendBroadcast(intent1);
						Intent intent2 = new Intent();
						intent2.setAction(Constant.CHANGEMYCASE);
						sendBroadcast(intent2);
						ToastHelper.showMsg(NewCaseDetailActivity.this, succString, false);
						finish();
//						changeGroupName(groupid, title);
//						if("3".equals(statusNum)){
//							exitDelGroup(groupId);
//						}
					}

					@Override
					public void onFailed(String failString) {
						ToastHelper.showMsg(NewCaseDetailActivity.this,
								failString, false);
//						finish();
					}
				});

	}
	
	public void exitDelGroup(String groupId){
		if (contactgroupService == null) {
			contactgroupService = new ContactGroupService(NewCaseDetailActivity.this);
		}
		
		contactgroupService.delGroup(groupId, new GetMultilParamsListener() {
			
			@Override
			public void onFinish(Object... params) {
				
			}
			
			@Override
			public void onFailed(String Message) {
				
			}
		});
		
	}
	
	private void changeGroupName(String groupid, final String groupname1) {
		if (contactgroupService == null) {
			contactgroupService = new ContactGroupService(this);
		}
		contactgroupService.changeGN(groupid, groupname1, new PostRecordResponseListener() {
			
			@Override
			public void onFinish(String succString) {
				Intent intent = new Intent();
				intent.putExtra("groupname1", groupname1);
				intent.setAction("name_group");
				setResult(6, intent);
				sendBroadcast(intent);
				finish();
			}
			
			@Override
			public void onFailed(String failString) {
				ToastHelper.showMsg(NewCaseDetailActivity.this,
						failString, false);
			}
		});
	}

	private void newcase() {

	}

	private void tv_jump_change_status() {
		Intent intent = new Intent();
		intent.setClass(this, CaseChangeStatusActivity.class);
		startActivityForResult(intent, Constant.CaseChangeStatus_CODE);
	}

	private void jump_priority() {
		Intent intent = new Intent();
		intent.setClass(this, CaseChangePriorityActivity.class);
		startActivityForResult(intent, Constant.CaseChangePriority_CODE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data != null) {
			if (Constant.CaseChangePriority_CODE == requestCode) {
				if (resultCode == Constant.CaseChangePriority_CODE) {
					String Priority = data
							.getStringExtra(Constant.CaseChangePriority);
					mTv_jump_priority.setText(Priority);
					if(Priority.equals("非常紧急")){
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority1));
					}else if(Priority.equals("紧急")){
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority2));
					}else if(Priority.equals("重要")){
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority3));
					}else if(Priority.equals("一般")){
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority4));
					}else if(Priority.equals("不重要")){
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority5));
					}else if(Priority.equals("不紧急不重要")){
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority6));
					}
				}
			} else if (requestCode == Constant.CaseChangeStatus_CODE) {
				if (resultCode == Constant.CaseChangeStatus_CODE) {
					String Status = data
							.getStringExtra(Constant.CaseChangeStatus);
					mTv_jump_change_status.setText(Status);
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	public String setPriority(String str){
		String str1 = "";
		if(str.equals("非常紧急")){
			str1 = "1";
		}else if(str.equals("紧急")){
			str1 = "2";
		}else if(str.equals("重要")){
			str1 = "3";
		}else if(str.equals("一般")){
			str1 = "4";
		}else if(str.equals("不重要")){
			str1 = "5";
		}else if(str.equals("不紧急不重要")){
			str1 = "6";
		}
		return str1;
	}
	
	public String setPriority1(String str, TextView textView){
		String str1 = "";
		if(str.equals("1")){
			str1 = "非常紧急";
			textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority1));
		}else if(str.equals("2")){
			str1 = "紧急";
			textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority2));
		}else if(str.equals("3")){
			str1 = "重要";
			textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority3));
		}else if(str.equals("4")){
			str1 = "一般";
			textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority4));
		}else if(str.equals("5")){
			str1 = "不重要";
			textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority5));
		}else if(str.equals("6")){
			str1 = "不紧急不重要";
			textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority6));
		}
		textView.setText(str1);
		return str1;
	}
}
